package 代码随想2._6栈与队列;

/**
 * @author XXX
 * @date 2024-01-17 16:44
 */

import java.util.Stack;

/**
 * https://leetcode.cn/problems/implement-queue-using-stacks/description/
 */
public class _1用栈实现队列 {
    class MyQueue {
        Stack<Integer> s1;
        Stack<Integer> s2;
        public MyQueue() {
            this.s1 = new Stack<>();
            this.s2 = new Stack<>();
        }

        public void push(int x) {
            s1.push(x);
        }

        public int pop() {
            while (!s1.isEmpty())
                s2.push(s1.pop());
            int pop = s2.pop();
            while (!s2.isEmpty())
                s1.push(s2.pop());
            return pop;
        }

        public int peek() {
            while(!s1.isEmpty())
                s2.push(s1.pop());
            int val = s2.peek();
            while(!s2.isEmpty())
                s1.push(s2.pop());
            return val;
        }

        public boolean empty() {
            return s1.isEmpty() && s2.isEmpty();
        }
    }
}
